package cn.school.servlet;


import cn.school.entity.Admin;
import cn.school.service.AdminService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        String name=request.getParameterValues("userName")[0];
        String name = request.getParameter("userName");
        String pass = request.getParameter("userPass");

        Cookie[] cr = request.getCookies();
        Cookie cfnum = null;
        for (Cookie c : cr) {
            if (c.getName().equals("cfnum")) {
                cfnum = c;
            }
        }
        if (cfnum == null) {
            cfnum = new Cookie("cfnum", "0");
        }
        cfnum.setMaxAge(3600 * 24 * 7);
        int loginfault = Integer.parseInt(cfnum.getValue());
        if (loginfault < 3) {
            Admin login = AdminService.INST.login(name, pass);

            if (login != null) {
                //用户信息放入session
                HttpSession session = request.getSession();//获得会话对象
                session.setAttribute("USER_LOGIN", login);
                //把用户名存入cookie
                Cookie loginname = new Cookie("loginname", name);
                Cookie loginpass = new Cookie("loginpass", pass);
                loginname.setMaxAge(3600 * 24 * 7);
                loginpass.setMaxAge(3600 * 24 * 7);
                response.addCookie(loginname);//!!!!!!!!!非常重要
                response.addCookie(loginpass);
                response.sendRedirect("main.jsp");
                return;
            } else {
                cfnum.setValue(String.valueOf(loginfault + 1));
                cfnum.setMaxAge(86400);
                response.addCookie(cfnum);
                request.setAttribute("msg", "错误的用户名或者密码！");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("msg", "今日登陆次数已达上限！");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
    }
//        if (login != null) {
//            //用户信息放入session
//            HttpSession session = request.getSession();//获得会话对象
//            session.setAttribute("USER_LOGIN", login);
//            //把用户名存入cookie
//            Cookie loginname = new Cookie("loginname", name);
//            Cookie loginpass = new Cookie("loginpass", pass);
//            loginname.setMaxAge(3600 * 24 * 7);
//            loginpass.setMaxAge(3600 * 24 * 7);
//            response.addCookie(loginname);//!!!!!!!!!非常重要
//            response.addCookie(loginpass);
//            response.sendRedirect("main.jsp");
//            return;
//        } else {
//            Cookie[] cr=request.getCookies();
//            Cookie cfnum=null;
//            for(Cookie c:cr){
//                if(c.getName().equals("cfum")){
//                    cfnum=c;
//                }
//            }
//            if(cfnum==null) {
//                cfnum = new Cookie("cfnum","0");
//            }
//            cfnum.setMaxAge(3600 * 24 * 7);
//            int loginfault=Integer.parseInt(cfnum.getValue());
//            cfnum.setValue(String.valueOf(loginfault+1));
//            response.addCookie(cfnum);
//            if (loginfault <3) {
//                request.setAttribute("msg", "错误的用户名或者密码！");
//                request.getRequestDispatcher("/login.jsp").forward(request, response);
//            } else {
//                request.setAttribute("msg", "今日登陆次数已达上限！");
//                request.getRequestDispatcher("/login.jsp").forward(request, response);
//            }
//        }
//    }


    //else{
//            HttpSession session=request.getSession();
//            int fnum=(int)request.getAttribute("fnum");
//            session.setAttribute("FAULTNUM",fnum);
//            if(fnum<3){
//                fnum++;
//                request.setAttribute("fnum",fnum);
//                session.setAttribute("FAULTNUM",fnum);
//            }else{
//                Cookie loginname=new Cookie("loginname",null);
//                loginname.setMaxAge(3600*24*7);
//                response.addCookie(loginname);//!!!!!!!!!非常重要
//                response.sendRedirect("main.jsp");
//                request.setAttribute("msg","登陆已经达到上限！");
//                request.getRequestDispatcher("/login.jsp").forward(request,response);
//            }
//        }


//        if(name.startsWith("java76")&&pass.startsWith("123")){
//            request.getSession().setAttribute("USER_LOGIN",name);
//            //name放入cookie...
//            //一组键值对
//            Cookie c=new Cookie("loginname",name);
//            c.setMaxAge(86400*7);
//            response.addCookie(c);
//
//            response.sendRedirect("main.jsp");
//            return;
//        }
//        request.setAttribute("msg","错误的用户名或者密码！");
//        request.getRequestDispatcher("/login.jsp").forward(request,response);
//    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}